#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main()
{
    int i,p1,p2, pid, fd[2];
    char buf[80];
    char buf1[80];
    pipe(fd);
    while((p1=fork())==-1);
    if(p1==0)
    {
        sprintf(buf,"Message of child_1");
        write(fd[1],buf,80);
        printf("Child_1 write an message to pipe!\n");
        for( i=0; i<99999; i++);
        exit(0);
    }
    else
    {
        while((p2=fork())==-1);
        if(p2==0)
        {
            sprintf(buf,"Message of child_2");
            write(fd[1],buf,80);
            printf("Child_2 write an message to pipe!\n");
            for(i=0; i<99999; i++);
            exit(0);
        }
        else
        {
            for(i=0; i<2; i++)
            {
                pid=wait(NULL);
                read(fd[0],buf1,80);
                if(pid==p1)
                    printf("Parent read an message of child_1:%s\n",buf1);
                else
                    printf("Parent read an message of child_2:%s\n",buf1);
                close(fd[0]);
                close(fd[1]);
                //printf("OVER\n");
            }
        }
        printf("OVER\n");
        return 0;
    }
}

